<?php
class User extends CI_Controller{
	/**
	 * 检验用户是否注册
	 * @param array userData 用户详情信息
	 * @param String 获取openid需要code
	 * @return openid 
	 */
	function is_Register(){
		if(!isset($_POST['code'])||!isset($_POST['userData'])){
			echo json_encode(array('code'=>201,"errMsg"=>"参数缺失"));die;
		}
		$this->load->model("WxConfig_model");
		$wxConfig = $this->WxConfig_model->get_one(array("id"=>1));
		if(!isset($wxConfig['appid'])||!isset($wxConfig['secret'])){
			log_message("error","缺少微信appid或secret");
			echo json_encode(array('code'=>201,"errMsg"=>"缺少微信appid或secret"));die;
		}
		$this->load->library("weixin",array("appid"=>$wxConfig['appid'],"secret"=>$wxConfig['secret']));
		$wxInfo = $this->weixin->getOpenid($_POST['code']);
		$userData = json_decode($_POST['userData'],true);
		$wxInfo = json_decode($wxInfo,true);
		$this->load->model("Users_model");
		$user = $this->Users_model->get_one(array("openid"=>$wxInfo['openid'],"delete_flag"=>0));
		//检验用户是否存在,如果不存在注册用户
		if(!$user){
			$data=array("nickName"=>$userData['nickName'],
					"openid"=>$wxInfo['openid'],
					"city"=>$userData['city'],
					"gender"=>$userData['gender'],
					"province"=>$userData['province'],
					"accaterUrl"=>$userData['avatarUrl'],
					"createtime"=>date("Y-m-d H:i:s",time())
			);
			$id = $this->Users_model->insert($data);
			if(!isset($id)){
				log_message("error","用户注册失败,注册数据:"+json_encode($data));
				echo json_encode(array('code'=>201,"errMsg"=>"用户注册失败"));die;
			}
		}
		echo json_encode(array('code'=>200,"data"=>array('openid'=>$wxInfo['openid']),"errMsg"=>""));die;
	}
	function qrcode(){
// 		$this->config->load("config");
// 		echo $this->config->item("base_url");die;
		$value="http://www.baidu.com";
		$Qr="code.png";
		$logo="http://localhost/ci/uploadfile/user/aci.jpg";
		$matrixPointSize=12;
		$errorCorrectionLevel="S";
		$this->load->library(array("CIQrcode"));
		$last="qrcode.png";
		$this->ciqrcode->generateQrcode($value,$Qr,$logo,$errorCorrectionLevel,$matrixPointSize,$last);
	}
	//获取优惠卷列表
	function couponList(){
		if(!isset($_POST['page'])||!isset($_POST['openid'])){
			echo json_encode(array('code'=>201,"errMsg"=>"参数缺失"));die;
		}
		$openid = trim($_POST["openid"]);
		$page = trim($_POST['page']);
		$this->load->model(array("Coupons_model","Coupon_info_model","Users_model"));
		$userInfo = $this->Users_model->get_one(array("openid"=>$openid));
		$sql = "select cin.id,cin.state,c.coupon_name,c.actual_price,c.fill_price,c.type,cin.act_code FROM t_sys_coupon_info cin LEFT JOIN t_sys_coupon c ON cin.couponid=c.id WHERE cin.delete_flag = 0 and cin.userid=".$userInfo["id"];
		$data = $this->Coupon_info_model->select_sql($sql);
		echo json_encode(array('code'=>200,"errMsg"=>"查询成功","data"=>$data));die;
		
	}
	//获取用户信息
	public function getUserInfo(){
		if(empty($_POST['openid'])){
			echo json_encode(array('code'=>201,"errMsg"=>"参数缺失"));die;
		}
		$this->load->model(array("Users_model"));
		$openid = trim($_POST["openid"]);
		$userInfo = $this->Users_model->get_one(array("openid"=>$openid));
		echo json_encode(array('code'=>200,"errMsg"=>"查询成功","data"=>$userInfo));die;
	}
	//兑换优惠卷
	public function receiveCoupon(){
		if(empty($_POST['code']) || empty($_POST["openid"])){
			echo json_encode(array('code'=>201,"errMsg"=>"参数缺失"));die;
		}
		$code = safe_replace(trim($_POST['code']));
 		$this->load->model(array("Users_model","Coupons_model","Coupon_info_model"));
 		$openid = trim($_POST["openid"]);
 		$userInfo = $this->Users_model->get_one(array("openid"=>$openid));
 		$couponInfo = $this->Coupon_info_model->get_one(array("act_code"=>$code));
 		if($couponInfo){
 			if($couponInfo["userid"]){
 				echo json_encode(array('code'=>201,"errMsg"=>"兑换码已使用"));die;
 			}else{
 				$flag = $this->Coupon_info_model->update(array("userid"=>$userInfo['id']),array("act_code"=>$code));
 				if($flag){
 					echo json_encode(array('code'=>200,"errMsg"=>"兑换成功"));die;
 				}else{
 					echo json_encode(array('code'=>201,"errMsg"=>"兑换失败"));die;
 				}
 			}
 		}else{
 			echo json_encode(array('code'=>201,"errMsg"=>"兑换码有误"));die;
 		}
	}
	/**
	 * 获取banner图
	 */
	public function getBanner(){
		$this->config->load("config");
		$images = $this->config->item("image_url");
		$topimg = array(array("image"=>$images."xiaochengxu/banner01.jpg"),array("image"=>$images."xiaochengxu/banner02.jpg"));
		$hot = array(array("img"=>$images."xiaochengxu/jiaquan.png"),array("img"=>$images."xiaochengxu/qingxiji.png"),array("img"=>$images."xiaochengxu/bingx.png"));
		echo json_encode(array('code'=>200,"errMsg"=>"成功","topimg"=>$topimg,"recomlist"=>$hot));die;
 		
	}
}